package org.despair2D.ui.puppet.supportClasses 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	
	import org.despair2D.core.ns_despair;
	use namespace ns_despair;
	
public class ImagePuppetComp extends PuppetComp
{
	
	public function ImagePuppetComp() 
	{
		super()
		m_img = new AutoSmoothingBitmap()
		this.addChild(m_img);
	}
	
	
	final override public function set rotation( v:Number ) : void 
	{
		if (m_rotation != v)
		{
			super.rotation = v;
			if (m_rotation != 0 && !m_internalTransform && !m_externalTransform)
			{
				this.makeTransform(true, false);
			}
			else if (m_rotation == 0 && m_scaleX == 1 && m_scaleY == 1 && !m_externalTransform)
			{
				this.makeTransform(false, false)
			}
		}
	}
	
	final override public function set scaleX( v:Number ) : void 
	{
		if (m_scaleX != v)
		{
			super.scaleX = v;
			if (m_scaleX != 0 && !m_internalTransform && !m_externalTransform)
			{
				this.makeTransform(true, false);
			}
			else if (m_rotation == 0 && m_scaleX == 1 && m_scaleY == 1 && !m_externalTransform)
			{
				this.makeTransform(false, false)
			}
		}
	}
	
	final override public function set scaleY( v:Number ) : void 
	{
		if ( m_scaleY != v)
		{
			super.scaleY = v;
			if (m_scaleY != 0 && !m_internalTransform && !m_externalTransform)
			{
				this.makeTransform(true, false);
			}
			else if (m_rotation == 0 && m_scaleX == 1 && m_scaleY == 1 && !m_externalTransform)
			{
				this.makeTransform(false, false)
			}
		}
	}

	final override ns_despair function makeTransform( smoothing:Boolean, external:Boolean ) : void
	{
		if (external)
		{
			m_externalTransform = smoothing
		}
		else
		{
			m_internalTransform = smoothing
		}
		
		if (!m_internalTransform && !m_externalTransform)
		{
			this.m_img.smoothing = false;
			//trace('[smoothing] - false')
		}
		else
		{
			this.m_img.smoothing = true;
			//trace('[smoothing] - true')
		}
	}
	
	override ns_despair function dispose() : void
	{
		super.dispose();
		
		m_img.dispose()
		m_externalTransform = m_internalTransform = false;
	}
	
	override ns_despair function recycle() : void
	{
		cachedImageList[cachedImageLength++] = this;
	}
	
	ns_despair static function getImagePuppetComp() : ImagePuppetComp
	{
		return (cachedImageLength > 0 ? cachedImageLength-- : 0) ? cachedImageList.pop() : new ImagePuppetComp();
	}
	
	
	ns_despair static var cachedImageList:Array = []
	
	ns_despair static var cachedImageLength:int;
	
	
	ns_despair var m_img:AutoSmoothingBitmap;

	ns_despair var m_externalTransform:Boolean, m_internalTransform:Boolean
}
}